/**
 * 
 */
package org.istt.web.action.file;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.istt.domain.Department;
import org.istt.domain.User;
import org.istt.service.DepartmentManager;
import org.istt.service.FileManager;
import org.istt.service.UserManager;
import org.istt.web.WebConstants;
import org.istt.web.helper.ExtendedPaginatedList;
import org.istt.web.helper.PaginateListFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author Huy
 * 
 */
public class UserFileAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7008734754260924416L;
	protected final Log log = LogFactory.getLog(getClass());

	private ExtendedPaginatedList fileList;
	private FileManager fileManager;
	private PaginateListFactory paginateListFactory;
	private Long userId;
	private String groupId;
//	private DateTime fromDate;
//	private DateTime toDate;
	private String fromDate;
	public String getFromDate() {
		return fromDate;
	}

	public void setFromDate(String fromDate) {
		this.fromDate = fromDate;
	}

	public String getToDate() {
		return toDate;
	}

	public void setToDate(String toDate) {
		this.toDate = toDate;
	}

	private String toDate;
	private DepartmentManager departmentManager;
	private UserManager userManager;
	
	@Autowired
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
	
	public List<User> getListUser() {
		if (groupId == null) {
			return new ArrayList<User>();
		} else {
			if (log.isDebugEnabled()) {
				log.debug("groupId is equal to " + groupId + " getting users");
			}
			return userManager.findUserByDeptId(NumberUtils.toLong(groupId));
		}
	}	
	
	public List<Department> getDepartments() {
		return departmentManager.getList();
	}
	
	@Autowired
	public void setDepartmentManager(DepartmentManager departmentManager) {
		this.departmentManager = departmentManager;
	}
	
	@Autowired
	public void setFileManager(FileManager fileManager) {
		this.fileManager = fileManager;
	}

	@Autowired
	public void setPaginateListFactory(PaginateListFactory paginateListFactory) {
		this.paginateListFactory = paginateListFactory;
	}

	public ExtendedPaginatedList getFileList() {
		return fileList;
	}

	public void setFileList(ExtendedPaginatedList fileList) {
		this.fileList = fileList;
	}
	
	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}
	
	public String getGroupId() {
		
		// fieldID has selected on form
		if (StringUtils.isEmpty(groupId)) {
			User loggedUser = (User)ActionContext.getContext().getSession().get(WebConstants.USER_INFO);
			groupId = loggedUser.getDepartment().getId().toString();
		}
		
		return groupId;
	}

	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	/*public DateTime getFromDate() {
		return fromDate;
	}

	public void setFromDate(DateTime fromDate) {
		this.fromDate = fromDate;
	}

	public DateTime getToDate() {
		return toDate;
	}

	public void setToDate(DateTime toDate) {
		this.toDate = toDate;
	}*/

	public String listUserFile() throws Exception {
		ExtendedPaginatedList paginatedList = paginateListFactory
				.getPaginatedListFromRequest(ServletActionContext.getRequest());

		Date f;
		Date t;
		try {
			f = StringUtils.isEmpty(fromDate) ? null : DateUtils.parseDate(
					fromDate, new String[] { getText("date.format") });
			
			t = StringUtils.isEmpty(toDate) ? null : DateUtils.parseDate(
					toDate, new String[] { getText("date.format") });
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			f = new Date();
			t = new Date();
		}
		
		fileList = ((FileManager) fileManager).getFileByUser(userId, f, t, paginatedList);

		return SUCCESS;
	}
}
